package com.longxi.tianjintianbao.ui.adapter.multitype;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.greedao.entity.User;
import com.longxi.tianjintianbao.listener.SelectUserListener;
import com.longxi.tianjintianbao.model.PropertyCompany;

import me.drakeet.multitype.ItemViewBinder;

/**
 * Created by cxw on 2018/2/20.
 */
public class UserViewBinder extends ItemViewBinder<User, UserViewBinder.ViewHolder> {

    SelectUserListener listener;

    public UserViewBinder(SelectUserListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_user, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull User user) {
        PropertyCompany propertyCompany = (PropertyCompany)PropertyCompany.getData2().get(String.valueOf(user.getIptype()));
        SpannableStringBuilder builder = new SpannableStringBuilder(user.username + " " + propertyCompany.getName());
        builder.setSpan(new AbsoluteSizeSpan(45), user.username.length() + 1,builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(new ForegroundColorSpan(holder.mTvName.getContext().getResources().getColor(R.color.color_text_hint)), user.username.length() + 1,builder.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        holder.mTvName.setText(builder);
        holder.mTvAddress.setText(user.getLoginUsername());

        holder.mLlBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onSuccess(user.id);
            }
        });

        holder.mIvTrash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onTrash(holder.getAdapterPosition());
            }
        });
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mLlBtn;
        TextView mTvName;
        TextView mTvAddress;
        ImageView mIvTrash;
        ViewHolder(View itemView) {
            super(itemView);
            mLlBtn = itemView.findViewById(R.id.llBtn);
            mTvName = itemView.findViewById(R.id.tvName);
            mTvAddress = itemView.findViewById(R.id.tvAddress);
            mIvTrash = itemView.findViewById(R.id.ivTrash);
        }
    }
}
